﻿using System;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Drawing;
using olierFrm.WebFormDev;
using olierFrm.DataBase.FrameWork.StdQuery;
using olierFrm.Extensions.EDataTable;
using olierFrm.WebFormDev.StdQuery;

namespace WebFormDev
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            ManageSystem.IsLogin();
            ManageSystem.SetTheme(Page);
        }
        StdQuerySQL dbStdQuery = new StdQuerySQL("RptClientPreSettleOrders");
        protected void Page_Load(object sender, EventArgs e)
        {

            var grid = new ASPxGridView();

            #region GRID的默认设置
            grid.SettingsPager.Summary.Text = "共{1}页，第{0}页（共{2}项）";
            //grid.SettingsPager.PrevPageButton.Text = "上一页";
            //grid.SettingsPager.NextPageButton.Text = "下一页";
            grid.SettingsText.EmptyDataRow = "没有数据显示";
            grid.SettingsText.GroupPanel = "拖动列标题到此处进行分组";
            grid.SettingsText.GroupContinuedOnNextPage = "（续下页）";
            grid.SettingsLoadingPanel.Text = "数据加载中，请稍候！";
            grid.Border.BorderStyle = BorderStyle.None;
            grid.SettingsCookies.StoreGroupingAndSorting = true;
            grid.Styles.Cell.Paddings.Padding = 1;
            grid.Styles.DetailCell.Paddings.Padding = 1;
            grid.Styles.DetailCell.Paddings.Padding = 1;
            grid.Styles.Header.Paddings.Padding = 2;
            grid.Styles.Footer.Paddings.Padding = 2;
            grid.Styles.GroupPanel.Paddings.Padding = 3;
            grid.Styles.PagerBottomPanel.Paddings.Padding = 0;
            grid.SettingsDetail.AllowOnlyOneMasterRowExpanded = true;
            grid.Settings.ShowHorizontalScrollBar = true;
            grid.Width = new Unit("100%");
            grid.Styles.EmptyDataRow.BackColor = Color.White;
            grid.SettingsPager.PageSize = 30;
            grid.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.Disabled;





            #endregion

            dbStdQuery.SetParam("BegDate", "2000-09-16");
            dbStdQuery.SetParam("EndDate", "2010-09-30");
            dbStdQuery.SetParam("Checked", "0");
            var dtSource = dbStdQuery.ExecuteSQL();
            grid.DataSource = dtSource;

            dtSource.AddDataRowIndex();
            //是否有SubQuery
            if (dbStdQuery.StdQuery.SubQuery != null && dbStdQuery.StdQuery.SubQuery.Trim().Length > 3)
            {
                SubQuery sq = new SubQuery(dbStdQuery.StdQuery.SubQuery);
                grid.SettingsDetail.ShowDetailRow = true;
                grid.KeyFieldName = dtSource.GetDataRowIndexFieldName();
                grid.DetailRowExpandedChanged += new ASPxGridViewDetailRowEventHandler(grid_DetailRowExpandedChanged);
                grid.Styles.DetailCell.Paddings.PaddingTop = 3;
            }

            grid.Settings.ShowGroupPanel = true;
            grid.DataBind();
            grid.ClientSideEvents.EndCallback = "function(){$('StdQueryGrid_DXMainTable').css('table-layout','auto');}";
            this.form1.Controls.Add(grid);
            //grid.Templates.Header = new HeaderRowTemplate();
        }

        /// <summary>
        /// 展开Grid时加载Detail的Template
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_DetailRowExpandedChanged(object sender, ASPxGridViewDetailRowEventArgs e)
        {
            var grid = (ASPxGridView)sender;
            grid.SetExpandRow(e.VisibleIndex);
            var it = new DetailRowTemplate();
            it.MasterStdQuery = dbStdQuery;
            it.MasterGrid = grid;
            grid.Templates.DetailRow = it;
        }

    }
}